package com.ambimmort.shj.service.generalfile;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Appdef;
import cn.net.greenet.db.bean.AppdefId;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.List;

/**
 * Created by 师梁 on 2015/7/30.
 */
@Service("add_AppService")
public class Add_AppService {
    //    @Autowired
//    private SessionFactory hibernateSessionFactory;
    ResultSet rs = null;
    BaseDao baseDao = null;
    String sql = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }

    public boolean add_App(String content) {
        JSONArray arr = JSONArray.fromObject(content);
        boolean result = false;
        try {
            Appdef appdef = null;
            AppdefId appdefid = null;
            for (int i = 0; i < arr.size(); i++) {
                JSONObject one = arr.getJSONObject(i);
                String appName = one.getString("AppName");
                String appType = one.getString("AppType");
                String appID = one.getString("AppID");
                sql = "select * from Appdef as w where w.id.name = '" + appName + "' "
                        + "and w.apptype.typeName='" + appType + "'"
                        + " and w.apptype.id =" + Integer.parseInt(appID);
//                List hfdws = session.createQuery(hql).list();
                Object[] params = {};
                rs = baseDao.executeSQL(sql, params);
                //如果新的当前行有效，则返回 true；如果不存在下一行，则返回 false
                if (!rs.next()) {
                    Object[] param = {Integer.parseInt(appID), appName, appdefid};
                    sql = "insert into appdef (id, name, AppType_id) values (?, ?, ?)";
                    baseDao.executeUpdate(sql, param);
                }
            }
//        JSONArray arr = JSONArray.fromObject(content);
//        boolean result = false;
//        Session session = hibernateSessionFactory.openSession();
//        session.clear();
//        try {
//            Appdef appdef = null;
//            AppdefId appdefid = null;
//            Transaction tx = session.beginTransaction();
//            for (int i = 0; i < arr.size(); i++) {
//                JSONObject one = arr.getJSONObject(i);
//                String appName = one.getString("AppName");
//                String appType = one.getString("AppType");
//                String appID = one.getString("AppID");
//                String hql = "from Appdef as w where w.id.name = '" + appName + "' "
//                        + "and w.apptype.typeName='" + appType + "'"
//                        + " and w.apptype.id ="+Integer.parseInt(appID);
//                List hfdws = session.createQuery(hql).list();
//                    如果此列表中没有元素，则返回 true
//                if (hfdws.isEmpty()) {
//                    appdef = new Appdef();
//                    appdefid = new AppdefId();
//                    appdefid.setAppTypeId(Integer.parseInt(appID));
//                    appdefid.setName(appName);
//                    appdef.setId(appdefid);
//                    session.save(appdef);
//                }
//            }
//            tx.commit();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        } finally {
//            session.close();
////            sf.close();
            close();
            return result;
        }
    }

}
